Operations
Operations are expressions that use operators to derive values from other values. AppleScript includes operators for performing arithmetic operations, comparing values, performing Boolean evaluations, and coercing values.The values from which operators derive values are called operands. Each operator can handle operands of specific classes, which are defined in the definition of the operator. For example, the operands for the addition (
+
) operator must belong to the class Integer or Real, while the operand for the
Not operator must belong to class Boolean. Certain operators work with operands from a variety of classes. For example, you can use the concatenation operator (&
) to join two strings, two lists, or two records.The result of each operation is a value of a particular class. For many operators, such as the equality operator (
=
) and the greater than operator (>
), the class of the result is always the same--in these cases, Boolean. For other operators, such as the concatenation operator (&), the class of the result depends on the class of the operands. For example, the result of concatenating two strings is a string, but the result of concatenating two integers is a list of integers.If you use an operator with operands of the wrong classes, AppleScript attempts to coerce the operands to the correct class, if possible. For example, the concatenation operator (&) works with strings, lists, or records. When AppleScript evaluates the following expression, it coerces the integer
66
to a string before concatenating it with the string"Route"
.
"Route " & 66 --result: "Route 66"When evaluating expressions containing operators, AppleScript checks the leftmost operand first. If the operand does not belong to one of the legal classes for the operator, AppleScript coerces it if possible. After coercing the leftmost operand or verifying that it belongs to a legal class, AppleScript checks the rightmost operand and coerces it (if necessary and possible) to be compatible with the leftmost operand. The exceptions to this rule are expressions with the Is Contained By, Equal, and Is Not Equal operators. AppleScript checks the rightmost operand first in expressions with the Is Contained By operator. AppleScript never coerces operands of the Equal and Is Not Equal operators.If AppleScript cannot coerce the operands, it returns an error. For example, the addition operator (+) works with numbers (integers and real numbers) only. If you attempt to evaluate an expression such as
3 + "cat"
, you'll get an error, because AppleScript cannot coerce"cat"
to a number.Operations can be performed either by AppleScript or by an application. The rule is that if the leftmost operand is a value, AppleScript performs the operation, and if the leftmost operand is a reference to an application object, the application performs the operation. For example, the comparison
"Hello" contains word 1 of document "Report"is performed by AppleScript, because the first operand is a string. Before performing the comparison, AppleScript must get the value of the first
word. In contrast, the comparison
word 1 of document "Report" contains "Hello"is performed by the application containing the document named Report.The Is Contained By operator is an exception to this rule. In expressions with the Is Contained By operator, AppleScript performs the operation if the rightmost operand is a value and the application performs the operation if the rightmost operand is a reference to an application object.
Table 6-1 summarizes the AppleScript operators. For each operator, it includes a brief description of the operation and lists the class (or classes) of the operands and the class (or classes) of the result. A few of the operators are characters that you type with modifier keys. For these operators, the keystrokes are shown in parentheses. The section following the table provides more information about how operators treat different classes of operands.
The sections following the table contain more detailed explanations and examples of operations.
AppleScript operators Operator Description and And. Binary logical operator that results in true
if both the operand to its left and the operand to its right aretrue
. Both of the operands must evaluate to Boolean values. When evaluating expressions containing the And operator, AppleScript checks the leftmost operand first.
If its value isfalse
, AppleScript does not evaluate
the rightmost operand, because it already knows the expression isfalse
. (This behavior is sometimes called short-circuiting.)
Class of operands: Boolean
Class of result: Booleanor Or. Binary logical operator that results in true
if either the operand to its left or the operand to its right istrue
. At least one of the operands must evaluate to a Boolean value. When evaluating expressions containing the Or operator, AppleScript checks the leftmost operand first.
If its value istrue
, AppleScript does not evaluate the rightmost operand, because it already knows the expression istrue
. (This behavior is sometimes called short-circuiting.)
Class of operands: Boolean
Class of result: Boolean& Concatenation. Binary operator that joins two values. If the operand to the left of the operator is a string, the result is a string. If the operand to the left of the operator is a record, the result is a record. If the operand to the left of the operator belongs to any other class, the result is
a list.
Class of operands: Boolean, Class Identifier, Constant, Data, Date, Integer, List, Real, Record, Reference, String
Class of result: List, Record, String=
is
equal
equals
[is] equal toEqual. Binary comparison operator that results in true
if the operand to its left and the operand to its right have the same value. The operands can be of any class. The method AppleScript uses to determine equality depends on the class of the operands.
Class of operands: Boolean, Class Identifier, Constant, Data, Date, Integer, List, Real, Record, Reference, String
Class of result: Boolean≠ (Option-equal sign)
is not
isn't
isn't equal [to]
is not equal [to]
doesn't equal
does not equalNot equal. Binary comparison operator that results in true
if the operand to its left and the operand to its right have different values. The operands can be of any class. The method AppleScript uses to determine equality depends on the class of the operands.
Class of operands: Boolean, Class Identifier, Constant, Data, Date, Integer, List, Real, Record, Reference, String
Class of result: Boolean>
[is] greater than
comes after
is not less than or equal [to]
isn't less than or equal [to]Greater than. Binary comparison operator that results in true
if the value of the operand to its left is greater than the value of the operand to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. The method AppleScript uses to determine which value is greater depends on the class of the operands.
Class of operands: Date, Integer, Real, String
Class of result: Boolean<
[is] less than
comes before
is not greater than or equal [to]
isn't greater than or equal [to]
Less than. Binary comparison operator that results in true
if the value of the operand to its left is less than
the value of the operand to its right. Both operands
must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left.
The method AppleScript uses to determine which value is greater depends on the class of the operands.
Class of operands: Date, Integer, Real, String
Class of result: Boolean≥ (Option-greater-than sign)
>=
[is] greater than or equal [to]
is not less than
isn't less than
does not come before
doesn't come beforeGreater than or equal to. Binary comparison operator that results in true
if the value of the operand to its left is greater than or equal to the value of the operand to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of
the operand to the left. The method AppleScript uses to determine which value is greater depends on the class
of the operands.
Class of operands: Date, Integer, Real, String
Class of result: Boolean≤ (Option-less-than sign)
<=
[is] less than or equal [to]
is not greater than
isn't greater than
does not come after
doesn't come afterLess than or equal to. Binary comparison operator that results in true
if the value of the operand to its left is less than or equal to the value of the operand to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. The method AppleScript uses to determine which value is greater depends on the class of the operands.
Class of operands: Date, Integer, Real, String
Class of result: Booleanstart[s] with
begin[s] withStarts with. Binary containment operator that results
intrue
if the list or string to its right matches the beginning of the list or string to its left. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left.
Class of operands: List, String
Class of result: Booleanend[s] with Ends with. Binary containment operator that results in true
if the list or string to its right matches the end of the list or string to its left. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left.
Class of operands: List, String
Class of result: Booleancontain[s] Contains. Binary containment operator that results in true
if the list, record, or string to its right matches any part of the list, record, or string to its left. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left.
Class of operands: List, Record, String
Class of result: Booleandoes not contain
doesn't containDoes not contain. Binary containment operator that results in true
if the list, record, or string to its right does not match any part of the list, record, or string to its left. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left.
Class of operands: List, Record, String
Class of result: Booleanis in
is contained byIs contained by. Binary containment operator that results in true
if the list, record, or string to its left matches
any part of the list, record, or string to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand
to the left of the operator to the class of the operand to the right.
Class of operands: List, Record, String
Class of result: Booleanis not in
is not contained by
isn't contained byIs not contained by. Binary containment operator that results in true
if the list, record, or string to its left does not match any part of the list, record, or string to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the left of the operator to the class of the operand to the right.
Class of operands: List, Record, String
Class of result: Boolean* Multiply. Binary arithmetic operator that multiplies the number to its left and the number to its right.
Class of operands: Integer, Real
Class of result: Integer, Real+ Plus. Binary arithmetic operator that adds the number or date to its left and the number or date to its right. Only integers can be added to dates. AppleScript interprets such an integer as a number of seconds.
Class of operands: Date, Integer, Real
Class of result: Date, Integer, Real- Minus. Binary or unary arithmetic operator. The binary operator subtracts the number to its right from the number or date to its left. The unary operator makes
the number to its right negative. Only integers can be subtracted from dates. AppleScript interprets such an integer as a number of seconds.
Class of operands: Date, Integer, Real
Class of result: Date, Integer, Real÷ (Option-slash)
/Division. Binary arithmetic operator that divides the number to its left by the number to its right.
Class of operands: Integer, Real
Class of result: Realdiv Integral division. Binary arithmetic operator that divides the number to its left by the number to its right and returns the integral part of the answer as its result.
Class of operands: Integer, Real
Class of result: Integermod Remainder. Binary arithmetic operator that divides the number to its left by the number to its right and returns the remainder as its result.
Class of operands: Integer, Real
Class of result: Integer, Real^ Exponent. Binary arithmetic operator that raises the number to its left to the power of the number to its right.
Class of operands: Integer, Real
Class of result: Realas Coercion. Binary operator that converts the operand to its left to the class listed to its right. Not all values can
be coerced to all classes. The coercions that AppleScript can perform are listed in "Coercing Values" on page 66. The additional coercions, if any, that applications can perform are listed in application dictionaries.
Class of operands: the operand to the right of the operator must be a class identifier; the operand to the left must be a value that can be converted to that class
Class of result: the class specified by the class identifier to the right of the operatornot Not. Unary logical operator that results in true
if the operand to its right isfalse
, andfalse
if the operand
to its right istrue
.
Class of operand: Boolean
Class of result: Boolean[a] ( ref [to] | reference to ) A Reference To. Unary operator that causes AppleScript to interpret the value to its right as a reference instead of getting its value. For more information about the A Reference To operator, see "The 'A Reference To' Operator" on page 153.
Class of operand: Reference
Class of result: Reference
Subtopics
- Operators That Handle Operands of Various Classes
- Operator Precedence
- Date-Time Arithmetic